import {Provider} from "./base";
import {ethers} from 'ethers';
import Config from '@/assets/js/config/load';
import store from "@/store/index";

const config = Config();

let abi = config.sicbo.abi;
let contractAddress = config.sicbo.address;
let provider = Provider.ETSC;
let contract = new ethers.Contract(contractAddress, abi, provider);

/*
 * 购买
 */
export async function buy({privateKey, value, eth}) {
    try {
        let wallet = new ethers.Wallet(privateKey, provider);
        let contractWithSigner = contract.connect(wallet);
        let overrides = {
            gasLimit: 1000000,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            value: ethers.utils.parseEther(value.toString()),
            chainId: 1811
        };
        for (let i in eth) {
            eth[i] = ethers.utils.parseEther(eth[i].toString());
        }
        let tx = await contractWithSigner.buy(eth, overrides);
        await tx.wait();
        return 1;
    } catch (e) {
        console.log(e);
        return 0;
    }

}

/*
 * 提现
 */
export async function withdraw({privateKey, rid, sub = 0}) {
    try {
        rid += sub;
        let wallet = new ethers.Wallet(privateKey, provider);
        // 获取pid
        let player = await getPlayer(wallet.address);
        let pid = player[0].toString();
        // 获取round
        let round = await getRoundByRid(rid);
        if (round.res.toString() == "0") return 0;

        let num1 = Number(round.res.toString().substring(0, 1));
        let num2 = Number(round.res.toString().substring(1, 2));
        let num3 = Number(round.res.toString().substring(2, 3));

        // 判断是否提现
        let playerRound = await getPlayerRound(pid, rid);
        if (playerRound.isWithdraw) return 1;

        // 判断是否中奖
        let isWin = (num1 == num2 && num1 == num3 && Number(playerRound.same.toString()) > 0)
            || ((num1 + num2 + num3) > 10 && Number(playerRound.big.toString()) > 0)
            || ((num1 + num2 + num3) <= 10 && Number(playerRound.small.toString()) > 0)
            || (((num1 + num2 + num3) % 2) == 1 && Number(playerRound.single.toString()) > 0)
            || (((num1 + num2 + num3) % 2) == 0 && Number(playerRound.double.toString()) > 0);
        if (!isWin) {
            if (sub == 0) {
                return await withdraw({privateKey, rid, sub: 1})
            } else {
                return 1;
            }
        }

        let contractWithSigner = contract.connect(wallet);
        let overrides = {
            gasLimit: 354158,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            // value: ethers.utils.parseEther('0.0001'),
            chainId: 1811
        };
        let tx = await contractWithSigner.withdraw(rid, overrides);
        await tx.wait();
        return 2;
    } catch (e) {
        console.log(e);
        return 0;
    }
}

/*
 * 获取最新的场次信息
 */
export async function getRound() {
    let rID = await contract.rID_();

    let round = await contract.round_(rID.toString() - 1);

    let time = await contract.gameTime_();

    // let localTime = Date.parse(new Date()) / 1000;

    let res = round.res.toString();
    store.dispatch('sicbo/aRound', {
        id: rID,
        num1: parseInt(res / 100),
        num2: parseInt(res % 100 / 10),
        num3: parseInt(res % 10),
        lastTime: parseInt(time) + parseInt(round.ended.toString())
    })
}

/*
 * 根据场次id获取场次信息
 */
export async function getRoundByRid(rid) {
    return await contract.round_(rid);
}

/*
 * 获取用户信息
 */
export async function getPlayer(address) {
    return await contract.getPlayerInfoByAddress(address);
}

/*
 * 获取用户的场次信息
 */
export async function getPlayerRound(pid, rid) {
    return await contract.plyrRound_(pid, rid);
}
